<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class IndexController extends Controller
{
	// 显示前台首页
	public function getIndex(Request $request){

		$wz = DB::table('webcon')->get();
        // dd($wz[0]->wzsw == 1);
        if($wz[0]->wzsw !== 1){
          return view('home.weihu.weihu');
        }
        //dd($ww);


		$peizhi = DB::table('webcon')->first();
        session(['peizhi'=>$peizhi]);
		//查询友链
		$yl=DB::table('frilk')->get();
		$a=self::catePid(0);
		// 新上图书
		$cates=DB::table('goods')->join('cate','cate.id','=','goods.cateid')->select('goods.*','cate.name as catename')->where('state',1)->orderBy('id','desc')->take(6)->get();
		// 电子书
		// dd($cates);
		$dzs=DB::table('goods')->where('cateid',8)->take(10)->get();
		// 热门标签
		// $bq=DB::table('cate')->join('cate as cate2')->select('cate.id,cate.name,cate.pid')->where('cate.pid','cate2.id')->get();
		// $bq=$cates=DB::select('select *,concat(path,",",id) as paths from cate group by paths');
		$bq=DB::table('cate')->select('id')->where('pid','path')->get();

		return view('home.index.index',['cates'=>$cates,'dzs'=>$dzs,'bq','bq'=>$a,'yy'=>$yl]);
	}
    public static function catePid($pid){
      $res = DB::table('cate')->where('pid',$pid)->get();
      $data = [];
      foreach ($res as $k => $v) {
          $v->sub = self::catePid($v->id);
          $data[] = $v;
      } 
      return $data;
    }

	 // 前台收索
    public function getCable(Request $request){
    	$yl=DB::table('frilk')->get();
    	$name=$request->input('search');
    	// 查询数据库
    	$res=DB::table('goods')->select('goods.*','cate.name as catename')->where('goods.name','like','%'.$name.'%')->orWhere('goods.actro','like','%'.$name.'%')->join('cate','goods.cateid','=','cate.id')->paginate(6);
    	$num1=count($res);
    	// $res->num=$num;
    	// dd($num);
    	foreach($res as $k=>$v){
    		$num=DB::table('comment')->select('uid')->where('gid',$v->id)->distinct('uid')->get();
    		$plnum=count($num);    
    		$v->plnum=$plnum;
    	}
    	// dd($res,$num1);
    	// dd($res);
    	return view('/home/liebiao/show',['cates'=>$res,'yy'=>$yl,'num'=>$num1]);
    }   

    // 更多热门电子书
    public function getDzs(){
    	$yl=DB::table('frilk')->get();
    	// 电子书
    	$cates=DB::table('goods')->where('cateid','8')->paginate(6);
    	// dd($cates);
    	return view('/home/index/dzs',['yy'=>$yl,'cates'=>$cates]);
    }

}

?>